/*
 * Copyright (C) 2014 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * This program is the proprietary software of Realtek Semiconductor
 * Corporation and/or its licensors, and only be used, duplicated,
 * modified or distributed under the authorized license from Realtek.
 *
 * ANY USE OF THE SOFTWARE OTHER THAN AS AUTHORIZED UNDER
 * THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 *
 * Purpose :
 *
 * Feature : The file includes the following modules and sub-modules
 *           (1)
 */

#include "app_basic.h"
#include "feature_export_api.h"


typedef enum {
    API_OPT_FE_SLOT_ID_CHG_TO_GE_SLOT_ID,
    API_OPT_USE_USER_VIEW_ID,
    API_OPT_END
} api_opt_t;

static signed customOpt = -1;

unsigned int enable_port_remapping (va_list argp)
{
    UINT32 *pSlotId = NULL, api_opt_id = API_OPT_END;
    UINT32 *pPortId = NULL, count = 0;
    INT32   hit = -1;
    UINT8   portId;

    api_opt_id = va_arg(argp, UINT32);

    if (api_opt_id != customOpt)
    {
        va_end(argp);
        printf("%s() Not support cur_custom_type=%d\n", __FUNCTION__,customOpt);
        return FAL_OK;

    }
    switch (api_opt_id)
    {
        case API_OPT_FE_SLOT_ID_CHG_TO_GE_SLOT_ID:
            va_end(argp);
            gInfo.txc_cardhld_fe_slot_type_id = (TXC_CARDHLD_GE_SLOT_TYPE_ID);
            break;
        case API_OPT_USE_USER_VIEW_ID:
            pSlotId = va_arg(argp, UINT32 *);
            pPortId = va_arg(argp, UINT32 *);
            va_end(argp);
            if (!pSlotId || !pPortId)
               return FAL_FAIL;

            count = *pPortId;
            for (portId = 0; portId < RTK_MAX_NUM_OF_PORTS; portId++)
            {
                if (TXC_CARDHLD_ETH_FE_SLOT == *pSlotId &&
                       OMCI_ETH_PORT_FE != gInfo.devCapabilities.ethPort[portId].portType)
                       continue;

                if (TXC_CARDHLD_ETH_GE_SLOT == *pSlotId &&
                       OMCI_ETH_PORT_GE != gInfo.devCapabilities.ethPort[portId].portType)
                       continue;

                if (TXC_CARDHLD_ETH_2P5GE_SLOT == *pSlotId &&
                       OMCI_ETH_PORT_2P5GE != gInfo.devCapabilities.ethPort[portId].portType)
                       continue;

                if (TXC_CARDHLD_ETH_XE_SLOT == *pSlotId &&
                       OMCI_ETH_PORT_XE != gInfo.devCapabilities.ethPort[portId].portType)
                       continue;

                hit++;

                if (hit == count)
                {
                    *pPortId = gInfo.devCapabilities.ethPort[portId].portIdInType;

                    return FAL_OK;
                }
            }
            break;
        default:
            va_end(argp);
            return FAL_FAIL;
    }

    return FAL_OK;
}

unsigned int feature_module_opt_init(unsigned int moduleID, ... )
{
	FAL_ERROR_CODE	ret;
    va_list argPtr;

    va_start(argPtr, moduleID);
    customOpt = va_arg(argPtr, signed);
    va_end (argPtr);

    ret = feature_api_register(
            FEATURE_API_ME_00020000, moduleID, &enable_port_remapping);
    if (FAL_OK != ret)
        return FAL_FAIL;

    if(-1 == customOpt)
        customOpt = API_OPT_USE_USER_VIEW_ID;        

    return FAL_OK;
}

unsigned int feature_module_exit(void)
{
	feature_api_unregister(FEATURE_API_ME_00020000);

    return FAL_OK;
}

